import os
import time
from PIL import Image
# 图片名随机生成器
def randomImgName(n,formatimg="jpg"):
    return "img{}_{}.{}".format(str(time.time()).replace(".","")[:-4],n,formatimg)

path=os.path.join(".","dataset","hastrack")
print(os.listdir(path))
# ['datasetsplit.py', 无裂缝  有裂缝  有裂缝标签, 'ModifyRoad.py', , 'train_test_vail.csv']

hascrak=['有裂缝']
hasnocrak=['无裂缝']

# 创建一个新的图片文件，进行重新文件命名
if not os.path.exists(os.path.join(path,"images")):
    os.mkdir(os.path.join(path,"images"))
width, height=300,300
# 有裂缝
for dirname in hascrak:
    for imgname in os.listdir(os.path.join(path,dirname)):
        img=Image.open(os.path.join(path,dirname,imgname))
        img = img.resize((width, height), Image.ANTIALIAS)
        img.save(os.path.join(path,"images",randomImgName(1)))

# 无裂缝
for dirname in hasnocrak:
    for imgname in os.listdir(os.path.join(path,dirname)):
        img=Image.open(os.path.join(path,dirname,imgname))
        img = img.resize((width, height), Image.ANTIALIAS)
        img.save(os.path.join(path,"images",randomImgName(0)))